package com.zpark.updater;

import com.zpark.entity.HistoryData;
import com.zpark.entity.SuccessData;

import java.util.ArrayList;
import java.util.List;

public class DeviceUpdater extends Updater {

    private int totalCount=10;//只保留最近的10个设备信息

    public DeviceUpdater(int totalCount) {
        this.totalCount = totalCount;
    }

    @Override
    public void update(SuccessData successData, HistoryData historyData, UpdaterChain updaterChain) {
        doUpate(successData.getDeviceInformation(),historyData);
        updaterChain.doChain(successData, historyData);
    }

    private void doUpate(String deviceInformation, HistoryData historyData) {
//        System.out.println(deviceInformation);
        List<String> historyDeviceInformations = historyData.getHistoryDeviceInformations();
        if(historyDeviceInformations==null){//第一次
            historyDeviceInformations=new ArrayList<>();
        }

        historyDeviceInformations.add(deviceInformation);

        if(historyDeviceInformations.size()>10){
            historyDeviceInformations.remove(0);
        }

        historyData.setHistoryDeviceInformations(historyDeviceInformations);

    }

    public static void main(String[] args) {
        A a = new A();
        List<String> l = a.getL();
        if(l==null){
            l=new ArrayList<>();
        }
        l.add("abc");

        a.setL(l);//

        System.out.println(a.getL());

    }

    static class A{
        private List<String> l;

        public List<String> getL() {
            return l;
        }

        public void setL(List<String> l) {
            this.l = l;
        }
    }
}
